<template>
  <view class="container">
    <uni-forms ref="form" :modelValue="form" labelWidth="120px" labelPosition="top">
      <view class="hidden-title">培训教育</view>
      <view class="uni-container">
        <uni-table ref="table" border stripe emptyText="暂无更多数据" @selection-change="selectionChange">
          <uni-tr>
            <uni-th width="50" align="center">序号</uni-th>
            <uni-th width="150" align="center">培训教育</uni-th>
          </uni-tr>
          <uni-tr v-for="(item, index) in form.deviceExamineTrainEntityList" :key="index">
            <uni-td>{{ index + 1 }}</uni-td>
            <uni-td>{{ item.trainMatter }}</uni-td>
          </uni-tr>
        </uni-table>
      </view>
      <view class="background-white">
        <uni-forms-item name="opinionType" label="验收意见" class="acceptance" required>
          <uni-data-checkbox v-model="form.opinionType" :localdata="opinions"></uni-data-checkbox>
        </uni-forms-item>
      </view>
      <view class="background-white bor-top" v-if="form.opinionType === 'REPULSE'">
        <uni-forms-item name="targetNodeId" label="选择回退节点">
          <cu-cell
            title="回退节点"
            required
            link
            placeholder="请选择"
            :description="taskNode"
            @click="rollbackClick()"
          ></cu-cell>
        </uni-forms-item>
      </view>
      <view class="background-white bor-top">
        <uni-forms-item name="opinion" label="审查意见说明">
          <uni-easyinput
            type="textarea"
            v-model="form.opinion"
            placeholder="简要说明审批意见"
            maxlength="50"
          ></uni-easyinput>
        </uni-forms-item>
      </view>
    </uni-forms>
    <CuSelect
      v-if="form.opinionType === 'REPULSE'"
      ref="rollbackDict"
      title="选择回退节点"
      :options="backNodes"
      @update:value="rollbackConfirm"
    ></CuSelect>
    <view class="submit-group">
      <button class="u-button" :ripple="true" @click="submit">提交</button>
    </view>
  </view>
</template>

<script>
import CuCellGroup from '@/components/cu-cell/cu-cell-group.vue'
import UserSelector from '@/components/user-selector/index.vue'
import CuSelect from '@/components/CuSelect/index.vue'
import { train, getNode } from '@/api/repair/repair.js'
import { getDicts } from '@/api/system/dict.js'
import { getInfo } from '../../../../../../api/login'
import { mapGetters } from 'vuex'

export default {
  components: {
    CuCellGroup,
    UserSelector,
    CuSelect
  },
  data() {
    return {
      opinions: [
        { text: '合格', value: 'PASS' },
        { text: '不合格', value: 'REPULSE' }
      ],
      backNodes: [],
      id: '',
      procTaskId: '',
      form: {
        deviceExamineTrainEntityList: [],
        opinionType: '',
        targetNodeId: '',
        opinion: ''
      },
      taskNode: '',
      targetNodeTypeValue: '',
      rules: {
        opinionType: {
          rules: [
            {
              required: true,
              errorMessage: '验收意见不能为空'
            }
          ]
        },
        targetNodeId: {
          rules: [
            {
              required: true,
              errorMessage: '回退节点不能为空'
            }
          ]
        },
        opinion: {
          rules: [
            {
              required: true,
              errorMessage: '审查意见说明不能为空'
            }
          ]
        }
      }
    }
  },
  onReady() {
    this.$refs.form.setRules(this.rules)
  },
  onLoad(option) {
    if (!option.data) return
    let data = JSON.parse(option.data)
    this.id = data.id
    this.procTaskId = data.procTaskId
    this.getBackNode(data.procTaskId)
    this.getEduItem('dfm_device_examine_train')
  },
  computed: {
    ...mapGetters(['userId'])
  },
  methods: {
    getBackNode(procTaskId) {
      getNode(procTaskId).then((res) => {
        this.backNodes = res.data
        this.backNodes.map((m) => {
          m.label = m.nodeName
          m.value = m.nodeId
        })
      })
    },
    async getEduItem(dictType) {
      const { user } = await getInfo()
      const res = await getDicts(dictType)
      res.data.map((item) => {
        this.form.deviceExamineTrainEntityList.push({
          trainMatter: item.dictLabel,
          trainMatterKey: item.dictValue,
          trainer: user.name,
          trainerId: this.userId
        })
      })
    },
    rollbackClick() {
      this.$refs.rollbackDict.open()
    },
    rollbackConfirm(obj) {
      this.taskNode = obj.name
      this.form.targetNodeId = obj.value
      let match = this.backNodes.find((n) => {
        return n.value === obj.value
      })
      this.targetNodeTypeValue = match.nodeTypeValue
    },
    submit() {
      this.$refs.form.validate().then((res) => {
        let data = {
          deviceExamineTrainEntityList: this.form.deviceExamineTrainEntityList,
          deviceExamineWorkflowEntity: {
            examineId: this.id,
            opinionType: this.form.opinionType,
            opinion: this.form.opinion
          },
          targetNodeTypeValue: this.targetNodeTypeValue,
          targetNodeId: this.form.targetNodeId,
          id: this.id,
          procTaskId: this.procTaskId
        }
        train(data).then((res) => {
          this.$modal.showToast('处理成功')
          uni.$emit('refresh')
          uni.navigateBack()
        })
      })
    }
  }
}
</script>

<style lang="scss">
.container {
  margin-bottom: 200rpx;

  .background-white {
    background-color: #fff;
    padding: 20rpx 30rpx;
    box-sizing: border-box;

    .uni-forms-item__label {
      color: #333;
      font-size: 30rpx;
    }

    .bor-btm {
      display: flex;
      border-bottom: 1px solid #d5d7e3;
    }

    .photo {
      display: flex;
      justify-content: space-between;

      image {
        width: 214rpx;
        height: 214rpx;
      }
    }

    .text {
      padding-bottom: 20rpx;
      padding-top: 4rpx;
      color: #333;

      text {
        color: #666;
      }
    }
  }
}

.hidden-title {
  background: #f5f5f5;
  padding: 20rpx;
  color: #999;
}

.bor-top {
  border-top: 20rpx solid #f5f5f5;
}

.blue {
  color: #88b4c4;
}

.icon-style {
  width: 52rpx;
  height: 42rpx;
  padding-right: 10rpx;
}
.input {
  display: inline-block;
  width: 100%;
  background-color: #f5f5f5;
  padding: 20rpx;
  font-size: 28rpx;
  border-radius: 10rpx;
  height: 60rpx;
}
.acceptance {
  margin-bottom: 10rpx;
}
.acceptance-list {
  display: flex;

  .acceptance-item {
    display: flex;
    align-items: center;
    margin-left: 20rpx;
    color: #333;

    .icon-style {
      width: 50rpx;
      height: 40rpx;
      margin-right: 10rpx;
    }

    .content {
      flex: 1;
    }
  }
}
</style>
